﻿#include "switchwidget.h"
#include "ui_switchwidget.h"
#include <QStackedLayout>
#include <QWebEngineView>
#include "chatroom.h"
#include "dispatchwidget.h"

//#define TEST_WEBENGINE_IN_DEBUG
//#define WEBENGINE_USE_PRIVATE

SwitchWidget::SwitchWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SwitchWidget), stackedLayout(new QStackedLayout(this)), webButton(Q_NULLPTR)
{
    ui->setupUi(this);
    stackedLayout->addWidget(ui->page_web);
    stackedLayout->addWidget(ui->page_back);
    connect(stackedLayout, &QStackedLayout::currentChanged, this, &SwitchWidget::currentChanged);
    stackedLayout->setCurrentIndex(1);
}

SwitchWidget::~SwitchWidget()
{
    delete ui;
}

QWidget *SwitchWidget::getDynamicWidget() const
{
    return stackedLayout->widget(2);
}

bool SwitchWidget::closeDynamic()
{
    //关闭第三页
    //返回是否是需要set_video_list的窗口
    if(auto dynamic = getDynamicWidget())
    {
        //因为总是第三个，如果关掉会变成第二个，也就是空白页
        stackedLayout->removeWidget(dynamic);
        if(auto chatroom = qobject_cast<ChatRoom*>(dynamic))
        {
            //如果是聊天室
            delete dynamic;
            return true;
        }
        delete dynamic;
    }
    return false;
}

ChatRoom *SwitchWidget::getChatRoom() const
{
    return qobject_cast<ChatRoom*>(getDynamicWidget());
}

bool SwitchWidget::isChatRoomOpen(qint64 uniqueId) const
{
    auto current = getChatRoom();
    if(current && current->getUniqueId() == uniqueId)
    {
        //已经打开
        if(stackedLayout->currentWidget() != current)
            const_cast<QStackedLayout*>(stackedLayout)->setCurrentWidget(current);
        return true;
    }
    return false;
}

void SwitchWidget::addChatRoom(ChatRoom *newRoom)
{
    closeDynamic();
    stackedLayout->setCurrentIndex(stackedLayout->addWidget(newRoom));
}

bool SwitchWidget::isDispatchOpen() const
{
    if(auto dispatch = qobject_cast<DispatchWidget*>(getDynamicWidget()))
    {
        //已经打开
        if(stackedLayout->currentWidget() != dispatch)
            const_cast<QStackedLayout*>(stackedLayout)->setCurrentWidget(dispatch);
        return true;
    }
    return false;
}

void SwitchWidget::addDispatch(DispatchWidget *newDispatch)
{
    closeDynamic();
    stackedLayout->setCurrentIndex(stackedLayout->addWidget(newDispatch));
}

void SwitchWidget::createWeb()
{
#if !defined(QT_DEBUG) || defined(TEST_WEBENGINE_IN_DEBUG)
    auto widget_web = new QWebEngineView(ui->page_web);
    widget_web->setObjectName(QString::fromUtf8("widget_web"));
    widget_web->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    ui->verticalLayout_3->insertWidget(0, widget_web);
#ifdef WEBENGINE_USE_PRIVATE
    auto profile = new QWebEngineProfile();
    auto page = new QWebEnginePage(profile, widget_web);
    profile->setParent(page);
    widget_web->setPage(page);
#endif
    connect(widget_web, &QWebEngineView::loadStarted, this, [this](){
        ui->progressBar->setValue(0);
        ui->progressBar->show();
    });
    connect(widget_web, &QWebEngineView::loadProgress, this, [this](int progress){
        ui->progressBar->setValue(progress);
        ui->progressBar->setVisible(progress < 100);
    });
    connect(widget_web, &QWebEngineView::loadFinished, this, [this](bool ok){
        Q_UNUSED(ok)
        ui->progressBar->hide();
    });
#endif
}

void SwitchWidget::showWeb(QToolButton *button, const QUrl &url)
{
#if !defined(QT_DEBUG) || defined(TEST_WEBENGINE_IN_DEBUG)
    if(auto webview = qobject_cast<QWebEngineView*>(ui->verticalLayout_3->itemAt(0)->widget()))
    {
        bool changeButton = button != webButton;
        if(changeButton)
        {
            webButton = button;
            webview->load(url);
        }
        if(stackedLayout->currentWidget() != ui->page_web)
            stackedLayout->setCurrentWidget(ui->page_web);
        else if(changeButton)
            emit currentChanged(stackedLayout->currentIndex());
    }
#else
    Q_UNUSED(button)
    Q_UNUSED(url)
#endif
}

bool SwitchWidget::isWeb() const
{
    return stackedLayout->currentWidget() == ui->page_web;
}

QToolButton *SwitchWidget::currentButton() const
{
    if(stackedLayout->currentWidget() != ui->page_web)
        return Q_NULLPTR;
    return webButton;
}

bool SwitchWidget::isBlank() const
{
    auto current = stackedLayout->currentWidget();
    return current == Q_NULLPTR || current == ui->page_back;
}

QString SwitchWidget::currentTitle()
{
    return stackedLayout->currentWidget()->windowTitle();
}

bool SwitchWidget::closeCurrent()
{
    if(isWeb())
    {
#if !defined(QT_DEBUG) || defined(TEST_WEBENGINE_IN_DEBUG)
        if(auto webview = qobject_cast<QWebEngineView*>(ui->verticalLayout_3->itemAt(0)->widget()))
        {
            webButton = Q_NULLPTR;
            webview->load(QUrl("about:blank"));
        }
#endif
        stackedLayout->setCurrentWidget(ui->page_back);
    }
    else if (!isBlank())
        return closeDynamic();
    return false;
}
